package se.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.io.IOException;
import java.util.Iterator;

import javax.swing.BorderFactory;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.TitledBorder;

import org.pushingpixels.substance.api.skin.SubstanceRavenLookAndFeel;

import se.kernel.Cliente;
import se.kernel.Grupo;
import se.kernel.Servidor;

public class InterfazServidor extends JFrame
{

    private static final long serialVersionUID = 1L;
    private JPanel jContentPane = null;
    private PanelCorrerServidor panelCorrerServidor = null;
    private PanelClientes panelClientes = null;
    private PanelGrupos panelGrupos = null;
    private Servidor servidor;

    /**
     * This method initializes panelCorrerServidor
     * 
     * @return se.gui.PanelCorrerServidor
     */
    private PanelCorrerServidor getPanelCorrerServidor( )
    {
        if( panelCorrerServidor == null )
        {
            panelCorrerServidor = new PanelCorrerServidor( this );
        }
        return panelCorrerServidor;
    }

    /**
     * This method initializes panelClientes
     * 
     * @return se.gui.PanelClientes
     */
    private PanelClientes getPanelClientes( )
    {
        if( panelClientes == null )
        {
            panelClientes = new PanelClientes( );
            panelClientes.setBorder( BorderFactory.createTitledBorder( null, "Clientes", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, new Font( "Lucida Grande", Font.PLAIN, 13 ), Color.white ) );
        }
        return panelClientes;
    }

    /**
     * This method initializes panelGrupos
     * 
     * @return se.gui.PanelGrupos
     */
    private PanelGrupos getPanelGrupos( )
    {
        if( panelGrupos == null )
        {
            panelGrupos = new PanelGrupos( );
            panelGrupos.setBorder( BorderFactory.createTitledBorder( null, "Grupos", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, new Font( "Lucida Grande", Font.PLAIN, 13 ), Color.white ) );
        }
        return panelGrupos;
    }

    /**
     * @param args
     */
    public static void main( String[] args )
    {
        SwingUtilities.invokeLater( new Runnable( )
        {
            public void run( )
            {
                try
                {
                    UIManager.setLookAndFeel( new SubstanceRavenLookAndFeel( ) );
                    JFrame.setDefaultLookAndFeelDecorated( true );
                    JDialog.setDefaultLookAndFeelDecorated( true );
                }
                catch( UnsupportedLookAndFeelException e )
                {
                    e.printStackTrace( );
                }

                InterfazServidor thisClass = new InterfazServidor( );
                thisClass.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
                thisClass.setVisible( true );

            }
        } );

    }

    /**
     * This is the default constructor
     */
    public InterfazServidor( )
    {
        super( );
        initialize( );
        try
        {
            servidor = new Servidor( this );
        }
        catch( IOException e )
        {
            JOptionPane.showMessageDialog( this, "Error al cargar el archivo de persistencia", "Error", JOptionPane.ERROR_MESSAGE );
            e.printStackTrace( );
        }
        catch( ClassNotFoundException e )
        {
            e.printStackTrace( );
        }
    }

    /**
     * This method initializes this
     * 
     * @return void
     */
    private void initialize( )
    {
        this.setSize( 590, 450 );
        this.setResizable( false );
        this.setContentPane( getJContentPane( ) );
        this.setTitle( "Servidor Messenger" );
        this.setLocationRelativeTo( null );
    }

    /**
     * This method initializes jContentPane
     * 
     * @return javax.swing.JPanel
     */
    private JPanel getJContentPane( )
    {
        if( jContentPane == null )
        {
            jContentPane = new JPanel( );
            jContentPane.setLayout( new BorderLayout( ) );
            jContentPane.add( getPanelCorrerServidor( ), BorderLayout.NORTH );
            jContentPane.add( getPanelClientes( ), BorderLayout.CENTER );
            jContentPane.add( getPanelGrupos( ), BorderLayout.SOUTH );
        }
        return jContentPane;
    }

    public void correrServidor( int puerto )
    {
        if( !servidor.isServidorActivo( ) )
        {
            servidor.setPuerto( puerto );
            Thread hiloServidor = new Thread( servidor );
            hiloServidor.start( );
        }
    }

    public void pararServidor( )
    {
        if( servidor.isServidorActivo( ) )
            servidor.stopServidor( );
    }

    public void actualizarListaClientes( Iterator<Cliente> clientes )
    {
        panelClientes.actualizarListaClientes( clientes );
    }

    public void actualizarListaGrupos( Iterator<Grupo> grupos )
    {
        panelGrupos.actualizarListaGrupos( grupos );
    }

}
